package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.pojo.Course;
import com.offcn.pojo.CourseDetail;
import com.offcn.service.CourseService;
import com.offcn.util.PageUtil;
import com.offcn.util.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/course")
@MultipartConfig
public class CourseServlet extends BaseServlet {
    private CourseService courseService = new CourseService();
    ResultVo resultVo = null;

    /*文件上传*/
    public void uploadFile(HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        //获取上传文件对象
        Part part = request.getPart("file");
        //获取上传文件名称
        String fileName = part.getSubmittedFileName();
        //处理同名文件覆盖
        String newfileName = UUID.randomUUID().toString() + "_" + fileName;
        //设置目标服务器地址
        String path = "F:\\uploadpic\\";
        //执行文件上传
        part.write(path + newfileName);
        //根据上传文件类型设置返回值
        if (fileName.contains(".jpg") || fileName.contains(".png") || fileName.contains(".gif")) {
            //上传的是图片
            resultVo = new ResultVo(101, "图片上传成功", newfileName);
        } else {
            resultVo = new ResultVo(200, "视频上传成功", newfileName);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    //增加课程功能
    public void addCourse(HttpServletRequest request,
                          HttpServletResponse response) throws Exception {
        Course course = new Course();
        Map<String, String[]> map = request.getParameterMap();
        //给course赋值
        BeanUtils.populate(course, map);
        //调用后台增加功能
        int add = courseService.addCourse(course);
        if (add > 0) {
            resultVo = new ResultVo(200, "增加成功", null);
        } else {
            resultVo = new ResultVo(500, "增加失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    //图片及视频删除功能
    public void removeFile(HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        //获取需要删除的文件路径
        String filename = request.getParameter("filename");
        //获取文件的名称
        String file = filename.substring(filename.lastIndexOf("/") + 1, filename.length());
        String path = "F:\\uploadpic\\" + file;
        File removeFile = new File(path);
        boolean delete = removeFile.delete();
        if (delete) {
            if (filename.contains(".jpg") || filename.contains(".png") || filename.contains(".gif")) {
                //上传的是图片
                resultVo = new ResultVo(101, "图片删除成功", null);
            } else {
                resultVo = new ResultVo(102, "视频删除成功", null);
            }
        } else {
            resultVo = new ResultVo(500, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*分页查询方法*/
    public void allPageCourse(HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String pageSize = request.getParameter("pageSize");
        String currentPage = request.getParameter("currentPage");
        String search = request.getParameter("search");
        //调用service分页查询方法
        PageUtil<Course> coursePageUtil = courseService.allPageCourse(pageSize, currentPage, search);
        resultVo = new ResultVo(200, "查询成功", coursePageUtil);
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*批量删除课程*/
    public void deleteCourses(HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String cids = request.getParameter("cids");
        int i = courseService.deleteCourses(cids);
        if (i > 0) {
            resultVo = new ResultVo(200, "删除成功", null);
        } else {
            resultVo = new ResultVo(101, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*修改课程*/
    public void updateCourse(HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        Course course = new Course();
        Map<String, String[]> map = request.getParameterMap();
        BeanUtils.populate(course, map);
        int i = courseService.updateCourse(course);
        if (i > 0) {
            resultVo = new ResultVo(200, "修改课程成功", null);
        } else {
            resultVo = new ResultVo(500, "修改课程失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*增加课程明细*/
    public void addDetail(HttpServletRequest request,
                          HttpServletResponse response) throws Exception {
        CourseDetail courseDetail = new CourseDetail();
        Map<String, String[]> map = request.getParameterMap();
        BeanUtils.populate(courseDetail, map);
        int i = courseService.addDetail(courseDetail);
        if (i > 0) {
            resultVo = new ResultVo(200, "增加成功", null);
        } else {
            resultVo = new ResultVo(500,"增加失败",null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*查询所有课程*/
    public void queryAllCourse(HttpServletRequest request,
                               HttpServletResponse response) throws Exception{
        //调用后台方法
        List<Course> courseList = courseService.queryAllCourse();
        resultVo = new ResultVo(200,"查询成功",courseList);
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*根据*/

}
